package com.web.kdl.service.Minio;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.web.kdl.common.result.MessageConstants;
import com.web.kdl.daoV2.files.FilesDao;
import com.web.kdl.entity.files.Files;
import com.web.kdl.pojo.Vo.SvgVo;
import io.minio.ListObjectsArgs;
import io.minio.Result;
import io.minio.messages.Item;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @Author： shulibin
 * @Date： 2025/9/3 15:05
 * @Describe：
 */
@Service
public class FileServiceImpl implements FilesService{
    @Resource
    private FilesDao filesDao;
    @Override
    public void addOrUpdate(String url, String siteName, String flywheelName) {
        LambdaQueryWrapper<Files> wrapper = new LambdaQueryWrapper<Files>().eq(Files::getSite, siteName).eq(Files::getFlywheel, flywheelName);
        Files file1 = filesDao.selectOne(wrapper);
        if(file1!=null){
            file1.setUrl(url);
            filesDao.update(file1,wrapper);
            return;
        }
        Files file = new Files();
        file.setUrl(url);
        file.setSite(siteName);
        file.setFlywheel(flywheelName);
        if (!StringUtils.hasText(flywheelName)){
            file.setResourceType("SITE");
        }else{
            file.setResourceType("FLYWHEEL");
        }
        filesDao.insert(file);
    }

    @Override
    public String getFileUrl(String siteName, String flywheelName) {
        Files file = filesDao.selectOne(new LambdaQueryWrapper<Files>().eq(Files::getSite,siteName).eq(Files::getFlywheel,flywheelName));
        if(file!=null){
            return file.getUrl();
        }
        return MessageConstants.FILE_NOT_FOUND;
    }

    @Override
    public void deleteFile(String url) {
        filesDao.selectOne(new LambdaQueryWrapper<Files>().eq(Files::getUrl,url));
    }


}
